const { queryAll } = require('../../database/dbUtils')

//获取积分的数据和分页
const pointData=async (ctx:any)=>{
  let {college,stage,classroom,pageNumber,pageSize}=ctx.request.query
  console.log(pageNumber,pageSize);
  let sql=`select * from mpoint `
  //总数据
  let res=await queryAll(sql)
   //筛选数据
   let coll:any=[]
   res.forEach((item:any)=>{
     if(college==item.college){
       coll.push(item)
     }
   })
   //阶段
   let jie:any=[]
   coll.forEach((item:any)=>{
     if(stage==item.stage){
       jie.push(item)
     }
   })
   //班级
   let ban:any=[]
   jie.forEach((item:any)=>{
     if(classroom==item.class){
       ban.push(item)
     }
   })
   console.log(ban);
   
  let start=Number(pageNumber-1)*pageSize
  let end=Number(start+pageSize)
  // console.log(res);
  //截取到的数据
  let jieArr=ban.slice(start,end)
  // console.log(jieArr);
  
  ctx.status=200
  ctx.body={
    code:'1',
    msg:"获取成功",
    result:{res,jieArr,ban}
  }
}
//添加积分数据
const jipoint=async (ctx:any)=>{
  let {number,type,point,change}=ctx.request.query 
  let sql=`select * from mpoint `
  let res=await queryAll(sql)
  // console.log(number,type,point,change);
  let i=res.findIndex((item:any)=>{
    return item.number==number
  })
  // console.log(i);
  let list=JSON.parse(res[i].pointlist)
  list.push({time:'2022',event:type,point,changevent:change})
  list=JSON.stringify(list)
  console.log(list);
  await queryAll(`update mpoint set pointlist='${list}' where number='${1}'`)

  ctx.status=200
  ctx.body={
    code:"01",
    msg:"添加成功",
    result:res
  }
}
export{
  pointData,
  jipoint
}